<?php
/**
 * Created by PhpStorm.
 * User: vincentzhang
 * Date: 2018/5/3
 * Time: 09:14
 */

class RedisBiz
{

    private  $connections = array(); //定义一个对象池

//* $objRedisBiz = RedisBiz::singleton();
//* $objRedis = $objRedisBiz->getRedisInstance();
    //定义redis配置文件
    private  $servers = [
        // 主业务
        [
//            'host' => '10.66.189.214',
            'host' => '127.0.0.1',
            'port' => '6379',
            'password' => '',
            'timeout' => 0
        ],
        // 其他业务
        [
//            'host' => '10.66.201.128',
            'host' => '127.0.0.1',
            'port' => '6379',
            'password' => '',//Sharing2017
            'timeout' => 0
        ],
    ];

    /**
     * @var $_redis
     */
    private $_redis = null;

    /**
     * for singleton
     *
     * @var     object
     */
    private static $instance;

    // }}}
    // {{{ singleton()

    /**
     * singleton
     *
     * @return this
     */
    public static function singleton()
    {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }
        return self::$instance;
    }

    // }}}
    // {{{ construct()

    /**
     * constructor
     *
     * @return void
     */
    public function __construct()
    {
        $this->connect();
    }

    // }}}
    // {{{ getCacheName

    public function getCacheName($cache_name)
    {
        if (HTTP_RUN_AT_DEV) {
            $cache_name .= ".HTTP_RUN_AT_DEV";
        }
        return md5($cache_name);
    }

    // }}}
    // {{{ getRedisInstance()

    /**
     * @object redis
     */
    public function getRedisInstance()
    {
        if ($this->_redis) {
            return $this->_redis;
        }else{
            return $this->connect();
        }
    }

    // }}}

    /**
     * 定义添加redis配置方法
     */
    public function connect()
    {
        global $GLOBALS_OBJ_REDIS;

        if(is_object($GLOBALS_OBJ_REDIS)){
            $this->_redis = $GLOBALS_OBJ_REDIS;
            return $this->_redis;
        }

        // 默认选择第一个,可扩展hash集群
        $config = $this->servers[0];


        $this->_redis = $GLOBALS_OBJ_REDIS = new Redis();
        $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']);

        if ( ! $success)
        {
            log_message('error', 'Cache: Redis connection failed. Check your configuration.');
            return null;
        }

        if (isset($config['password']) && ! $this->_redis->auth($config['password']))
        {
            log_message('error', $config['host'].': Redis authentication failed.');
            return null;
        }

        return  $this->_redis;

    }

    public function hSet($key, $hashKey, $value)
    {
        return $this->_redis->hSet($key, $hashKey, $value);
    }

    public function hGet($key, $hashKey): string
    {
        return $this->_redis->hGet($key, $hashKey);
    }

    public function exists($key): bool
    {
        return $this->_redis->exists($key);
    }

    public function expire($key, $ttl): bool
    {
        return $this->_redis->expire($key, $ttl);
    }

    public function expireAt($key, $timestamp): bool
    {
        return $this->_redis->expireAt($key, $timestamp);
    }

    /**
     * Class destructor
     *
     * Closes the connection to Redis if present.
     *
     * @return	void
     */
    public function __destruct()
    {
        if ($this->_redis)
        {
            $this->_redis->close();
        }
    }

    /**
     * [__clone 禁用克隆]
     * @return
     */
    final private function __clone(){}


    public function __call($name, $arguments)
    {
        if ($name && $arguments){

            $this->_redis->$name($arguments);
        }
    }


}
